package com.careerraiser.xtra.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;



/** * MyRunnable will count the sum of the number from 1 to the parameter * countUntil and then write the result to the console. * <p> * MyRunnable is the task which will be performed * * @author Lars Vogel * */

class MyRunnable1 implements Runnable {
  private final long countUntil;

  MyRunnable1(long countUntil) {
    this.countUntil = countUntil;
  }

  @Override
  public void run() {
    long sum = 0;
    for (long i = 1; i < countUntil; i++) {
      sum += i;
    }
    System.out.println(sum);
  }
} 

public class ExecutorTest {
	  private static final int NTHREDS = 10;

	  public static void main(String[] args) {
	    ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
	    for (int i = 0; i < 500; i++) {
	      Runnable worker = new MyRunnable1(10000000L + i);
	      executor.execute(worker);
	    }
	    // This will make the executor accept no new threads
	    // and finish all existing threads in the queue
	    executor.shutdown();
	    // Wait until all threads are finish
	    try {
			executor.awaitTermination(10,TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    System.out.println("Finished all threads");
	  }
	} 